package com.example.demo.controller;

import com.example.demo.entity.Category;
import com.example.demo.service.CategoryService;
import com.example.demo.controller.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public ResponseResult<Category> createCategory(
            @RequestHeader("token") String token,
            @RequestParam("name") String name,
            @RequestParam(value = "status", defaultValue = "1") Integer status,
            @RequestParam(value = "order", defaultValue = "0") Integer order) {
        Category category = categoryService.createCategory(name, status, order);
        return ResponseResult.success(category);
    }

    @PostMapping("/{id}")
    public ResponseResult<Boolean> updateCategory(
            @RequestHeader("token") String token,
            @PathVariable("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "order", required = false) Integer order) {
        boolean result = categoryService.updateCategory(id, name, status, order);
        return ResponseResult.success(result);
    }

    @PostMapping("/{id}/update_status")
    public ResponseResult<Boolean> updateCategoryStatus(
            @RequestHeader("token") String token,
            @PathVariable("id") Integer id,
            @RequestParam("status") Integer status) {
        boolean result = categoryService.updateCategoryStatus(id, status);
        return ResponseResult.success(result);
    }

    @GetMapping("/{page}")
    public ResponseResult<?> getCategoryList(
            @RequestHeader("token") String token,
            @PathVariable("page") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "orderBy", defaultValue = "order") String orderBy) {
        return ResponseResult.success(categoryService.getCategoryList(page, limit, keyword, orderBy));
    }

    @PostMapping("/{id}/delete")
    public ResponseResult<Boolean> deleteCategory(
            @RequestHeader("token") String token,
            @PathVariable("id") Integer id) {
        boolean result = categoryService.deleteCategory(id);
        return ResponseResult.success(result);
    }
}
